package com.jinly.ryder.log.config;

import com.jinly.ryder.log.web.RealTimeLogHandshakeInterceptor;
import com.jinly.ryder.log.web.RealTimeLogWebSocketHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class RealTimeWebSocketConfig implements WebSocketConfigurer {

    @Bean
    public RealTimeLogHandshakeInterceptor realTimeLogHandshakeInterceptor() {
        return new RealTimeLogHandshakeInterceptor();
    }


    @Bean
    public RealTimeLogWebSocketHandler realTimeLogWebSocketHandler() {
        return new RealTimeLogWebSocketHandler();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(realTimeLogWebSocketHandler(), "ws/realTimeLog")
                .addInterceptors(realTimeLogHandshakeInterceptor())
                .setAllowedOrigins("*");
    }
}
